﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
namespace WinnerCMS.Model
{
    [DataContract]
    public class TreeNode
    {
        public TreeNode()
        {
            //this.State = "open";
            this.Children = new List<TreeNode>();
            this.Attributes = new CustomAttributes();
        }

        public TreeNode(string id, string text)
        {
            //this.State = "open";
            this.Children = new List<TreeNode>();
            this.Attributes = new CustomAttributes();
            this.Id = id;
            this.Text = text;
        }

        public TreeNode(string id, string text, string iconCls)
        {
            //this.State = "open";
            this.Children = new List<TreeNode>();
            this.Attributes = new CustomAttributes();
            this.Id = id;
            this.Text = text;
            this.IconCls = iconCls;
        }

        public TreeNode(string id, string text, string iconCls, string state)
        {
            this.Children = new List<TreeNode>();
            this.Attributes = new CustomAttributes();
            this.Id = id;
            this.Text = text;
            this.IconCls = iconCls;
            this.State = state;

        }

        [DataMember(Name = "id")]
        public string Id { get; set; }

        [DataMember(Name = "text")]
        public string Text { get; set; }

        [DataMember(Name = "type")]
        public string Type { get; set; }

        [DataMember(Name = "iconCls")]
        public string IconCls { get; set; }

        /// <summary>
        /// 是否为ajax
        /// </summary>
        /// <remarks>state: 节点状态, 'open' 或者 'closed', 默认是 'open'. 当设置为 'closed', 节点所有的子节点将从远程服务器站点加载</remarks>
        [DataMember(Name = "state")]
        public string State { get; set; }


        [DataMember(Name = "attributes")]
        public CustomAttributes Attributes { get; set; }


        [DataMember(Name = "children")]
        public List<TreeNode> Children { get; set; }

        /// <summary>
        /// 自定义字段
        /// </summary>
        [DataMember]
        public string CustomField1 { get; set; }

        /// <summary>
        /// 自定义字段
        /// </summary>
        [DataMember]
        public string CustomField2 { get; set; }

        /// <summary>
        /// 自定义字段
        /// </summary>
        [DataMember]
        public string CustomField3 { get; set; }

        /// <summary>
        /// 自定义字段
        /// </summary>
        [DataMember]
        public string CustomField4 { get; set; }

        /// <summary>
        /// 自定义字段
        /// </summary>
        [DataMember]
        public string CustomField5 { get; set; }

        public static TreeNode ToTreeNode(Memu mi)
        {
            if (mi == null)
            {
                throw new Exception("参数不完整。");
            }
            return new TreeNode
            {
                Id = mi.Id,
                Text = mi.Title,
                //State = mi.IsOpen ? "open" : "close",
                Attributes = new CustomAttributes() { Url = mi.Link }

            };
        }
    }
    [DataContract]
    public class CustomAttributes
    {
        [DataMember(Name = "url")]
        public string Url
        {
            get;
            set;
        }
    }
}
